using System.Collections;
using UnityEngine;

public class EnemyFSM : FSM
{
    public EnemyParameter parameter;  // 状态机参数
    protected override void Awake()
    {
        parameter.rb = GetComponent<Rigidbody2D>();
        parameter.animator = GetComponent<Animator>();
        parameter.sr = GetComponent<SpriteRenderer>();
        parameter.check = GetComponent<EnemyPhysicsCheck>();

        // 初始化各个状态，并添加到状态字典中
        states.Add(StateType.Idle, new EnemyIdleState(this, "Idle"));
        states.Add(StateType.MeleeAttack, new EnemyAttackState(this, "MeleeAttack"));
        states.Add(StateType.Chase, new EnemyChaseState(this, "Run"));
        states.Add(StateType.Death, new EnemyDeathState(this, "Death"));
        states.Add(StateType.Hit, new EnemyHitState(this, "Hit"));
        states.Add(StateType.Patrol, new EnemyPatrolState(this, "Walk"));
        states.Add(StateType.React, new EnemyReactState(this, "React"));

        base.Awake();
    }

    protected override void Update()
    {
        base.Update();
    }

    // 翻转角色朝向方法，使其朝向目标
    public void FlipTo(Transform target)
    {
        if (target != null)
        {
            if (transform.position.x > target.position.x)
            {
                parameter.facingDirection = -1;
                transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
            }
            else if (transform.position.x < target.position.x)
            {
                parameter.facingDirection = 1;
                transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
            }
        }
    }

    // 翻转角色
    public override void FlipTo()
    {
        parameter.facingDirection *= -1;

        transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
    }

    public void OnStartCoroutine(IEnumerator name)
    {
        StartCoroutine(name);
    }
}
